import java.awt.Dimension;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import etxt2db.gui.Etxt2DBGUIConnector;


public class Etxt2DBGUI extends JFrame implements ActionListener{

	protected JTabbedPane queryTextArea;
	protected JButton submitQuery;
	protected TextArea resutsTextArea;
	String beginTag = "<";
	String endTag = ">";
	boolean docPerFile = true;
	
	public void setBeginTag(String str){
		beginTag = str;
	}
	
	public void setEndTag(String str){
		endTag = str;
	}
	
	public void setDocPerFile(boolean bool){
		System.out.println("Changed doc per file to " + bool);
		docPerFile = bool;
	}
	
    public Etxt2DBGUI() {
        setTitle("E-txt2db Classification Interface");

        new TopMenuGUI(this);

        //BEGIN BASIC
        JPanel basic = new JPanel();
        basic.setLayout(new BoxLayout(basic, BoxLayout.Y_AXIS));
        add(basic);

        basic.add(Box.createVerticalGlue());

        //BEGIN QUERY SUBMISSION
        
        JPanel querySubmission = new JPanel();
        querySubmission.setLayout(new BoxLayout(querySubmission, BoxLayout.X_AXIS));
        
        querySubmission.add(Box.createHorizontalGlue());
        
        //BEGIN TEXT ZONE
        
        JPanel textZone = new JPanel();
        textZone.setLayout(new BoxLayout(textZone, BoxLayout.Y_AXIS));
        
        queryTextArea = new CloseableTabbedPane();
        queryTextArea.add(new FileTextArea(this,15,100),"<new>");
        
        //queryTextArea = new TextArea(5,50);
        
        textZone.add(queryTextArea);
        
        //BEGIN BUTTON ZONE
        
        JPanel buttonZone = new JPanel();
        buttonZone.setLayout(new BoxLayout(buttonZone, BoxLayout.X_AXIS));

        JButton ok = new JButton("Submit Query");
        ok.setActionCommand("submit");
        ok.addActionListener(this);
        submitQuery = ok;
        buttonZone.add(ok);
        
        //END BUTTON ZONE
        
        textZone.add(buttonZone);
        
        //END TEXT ZONE
        
        querySubmission.add(textZone);
        
        
        querySubmission.add(Box.createHorizontalGlue());
        
        //END QUERY SUBMISSION
        
        basic.add(querySubmission);
        basic.add(Box.createVerticalGlue());
        
        //BEGIN RESULT ZONE
        
        JPanel resultZone = new JPanel();
        resultZone.setLayout(new BoxLayout(resultZone, BoxLayout.X_AXIS));
        
        resultZone.add(Box.createHorizontalGlue());
        
        resutsTextArea = new TextArea(5,50);
        
        resultZone.add(resutsTextArea);
        
        resultZone.add(Box.createHorizontalGlue());
        
        //END RESULT ZONE
        
        basic.add(resultZone);
        
        basic.add(Box.createVerticalGlue());

        //END BASIC

        setSize(800, 600);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);

    }
    
    @Override
	public void actionPerformed(ActionEvent e) {
    	Etxt2DBGUIConnector connector = new Etxt2DBGUIConnector(resutsTextArea);
    	connector.setBeginTag(beginTag);
    	connector.setEndTag(endTag);
    	connector.setDocPerFile(docPerFile);
    	connector.interpretQuery(((FileTextArea)queryTextArea.getSelectedComponent()).getText());
	}

    public static void main(String[] args) {
    	try {
    	    // Set System L&F
            UIManager.setLookAndFeel(
                UIManager.getSystemLookAndFeelClassName());
        } 
        catch (UnsupportedLookAndFeelException e) {
           // handle exception
        }
        catch (ClassNotFoundException e) {
           // handle exception
        }
        catch (InstantiationException e) {
           // handle exception
        }
        catch (IllegalAccessException e) {
           // handle exception
        }

        new Etxt2DBGUI();
    }
}
